//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "PseudoRootFacade.h"
using namespace Cephei::QL::Models::Marketmodels::Models;
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/Models/Marketmodels/Models/CTSMMCapletCalibration.h>
#include <gen/QL/Models/Marketmodels/MarketModel.h>
using namespace Cephei::QL::Models::Marketmodels;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Models::CPseudoRootFacade::CPseudoRootFacade (Cephei::IVector<Cephei::QL::Math::IMatrix^>^ covariancePseudoRoots, Cephei::IVector<Double>^ rateTimes, Cephei::IVector<Double>^ initialRates, Cephei::IVector<Double>^ displacements) : CMarketModel(CPseudoRootFacade::typeid)
{
    CoVector<Cephei::QL::Math::IMatrix^>^ _CcovariancePseudoRoots;
    CoVector<Double>^ _CrateTimes;
    CoVector<Double>^ _CinitialRates;
    CoVector<Double>^ _Cdisplacements;
    try
    {
#ifdef HANDLE
        _phPseudoRootFacade = NULL;
#endif
        _CcovariancePseudoRoots = safe_cast<CoVector<Cephei::QL::Math::IMatrix^>^> (covariancePseudoRoots);
        _CcovariancePseudoRoots ->Lock ();
        INativeVector<Cephei::QL::Math::IMatrix^>^ _NCIcovariancePseudoRoots = _CcovariancePseudoRoots->getFeature (NativeFeature::Value);
        CMatrixVector^ _NCcovariancePseudoRoots = safe_cast<CMatrixVector^>(_NCIcovariancePseudoRoots);
        std::vector<QuantLib::Matrix>& _covariancePseudoRoots = static_cast<std::vector<QuantLib::Matrix>&> (_NCcovariancePseudoRoots->GetReference ());
        CoVector<Double>^ _CrateTimes = safe_cast<CoVector<Double>^> (rateTimes);
        _CrateTimes->Lock();
        INativeVector<Double>^ _NCIrateTimes = _CrateTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCrateTimes = safe_cast<CDoubleVector^>(_NCIrateTimes);
        std::vector<QuantLib::Rate>& _rateTimes = static_cast<std::vector<QuantLib::Rate>&> (_NCrateTimes->GetReference ());
        CoVector<Double>^ _CinitialRates = safe_cast<CoVector<Double>^> (initialRates);
        _CinitialRates->Lock();
        INativeVector<Double>^ _NCIinitialRates = _CinitialRates->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCinitialRates = safe_cast<CDoubleVector^>(_NCIinitialRates);
        std::vector<QuantLib::Rate>& _initialRates = static_cast<std::vector<QuantLib::Rate>&> (_NCinitialRates->GetReference ());
        CoVector<Double>^ _Cdisplacements = safe_cast<CoVector<Double>^> (displacements);
        _Cdisplacements->Lock();
        INativeVector<Double>^ _NCIdisplacements = _Cdisplacements->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCdisplacements = safe_cast<CDoubleVector^>(_NCIdisplacements);
        std::vector<QuantLib::Spread>& _displacements = static_cast<std::vector<QuantLib::Spread>&> (_NCdisplacements->GetReference ());
        _ppPseudoRootFacade = new boost::shared_ptr<QuantLib::PseudoRootFacade> (new QuantLib::PseudoRootFacade ( _covariancePseudoRoots,  _rateTimes,  _initialRates,  _displacements ));
        SetMarketModel (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppPseudoRootFacade));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcovariancePseudoRoots != nullptr) _CcovariancePseudoRoots->Unlock();
        if (_CrateTimes != nullptr) _CrateTimes->Unlock();
        if (_CinitialRates != nullptr) _CinitialRates->Unlock();
        if (_Cdisplacements != nullptr) _Cdisplacements->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Models::CPseudoRootFacade::CPseudoRootFacade (Cephei::QL::Models::Marketmodels::Models::ICTSMMCapletCalibration^ calibrator) : CMarketModel(CPseudoRootFacade::typeid)
{
    CCTSMMCapletCalibration^ _Ccalibrator;
    try
    {
#ifdef HANDLE
        _phPseudoRootFacade = NULL;
#endif
        _Ccalibrator = safe_cast<CCTSMMCapletCalibration^> (calibrator);
        _Ccalibrator->Lock();
        boost::shared_ptr<QuantLib::CTSMMCapletCalibration> _calibrator = static_cast<boost::shared_ptr<QuantLib::CTSMMCapletCalibration>> (_Ccalibrator->GetShared ()); 
        _ppPseudoRootFacade = new boost::shared_ptr<QuantLib::PseudoRootFacade> (new QuantLib::PseudoRootFacade ( _calibrator ));
        SetMarketModel (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppPseudoRootFacade));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccalibrator != nullptr) _Ccalibrator->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Models::CPseudoRootFacade::CPseudoRootFacade (boost::shared_ptr<QuantLib::PseudoRootFacade>& childNative, Object^ owner) : CMarketModel(CPseudoRootFacade::typeid)
{
#ifdef HANDLE
	_phPseudoRootFacade = NULL;
#endif
	_ppPseudoRootFacade = &childNative;
    _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppPseudoRootFacade));
}
Cephei::QL::Models::Marketmodels::Models::CPseudoRootFacade::CPseudoRootFacade (QuantLib::PseudoRootFacade& childNative, Object^ owner) : CMarketModel(CPseudoRootFacade::typeid)
{
#ifdef HANDLE
	_phPseudoRootFacade = NULL;
#endif
	_ppPseudoRootFacade = new boost::shared_ptr<QuantLib::PseudoRootFacade> (&childNative);
    _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppPseudoRootFacade));
    _PseudoRootFacadeOwner = owner;
    _MarketModelOwner = owner;
}

Cephei::QL::Models::Marketmodels::Models::CPseudoRootFacade::CPseudoRootFacade (CPseudoRootFacade^ copy) : CMarketModel(CPseudoRootFacade::typeid)
{
#ifdef HANDLE
	_phPseudoRootFacade = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppPseudoRootFacade = new boost::shared_ptr<QuantLib::PseudoRootFacade> (copy->GetShared());
        _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppPseudoRootFacade));
    }
}
Cephei::QL::Models::Marketmodels::Models::CPseudoRootFacade::CPseudoRootFacade (System::Type^ t) : CMarketModel(CPseudoRootFacade::typeid)
{
#ifdef HANDLE
	_phPseudoRootFacade = NULL;
#endif
	if (!t->IsSubclassOf(CPseudoRootFacade::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Models::CPseudoRootFacade::CPseudoRootFacade (QuantLib::Handle<QuantLib::PseudoRootFacade>& childNative, Object^ owner)  : CMarketModel(CPseudoRootFacade::typeid)
{
	_phPseudoRootFacade = &childNative;
	_ppPseudoRootFacade = &static_cast<boost::shared_ptr<QuantLib::PseudoRootFacade>>(childNative.currentLink());
    _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppPseudoRootFacade));
    _PseudoRootFacadeOwner = owner;
}
Cephei::QL::Models::Marketmodels::Models::CPseudoRootFacade::CPseudoRootFacade (QuantLib::Handle<QuantLib::PseudoRootFacade> childNative)  : CMarketModel(CPseudoRootFacade::typeid)
{
	_phPseudoRootFacade = &childNative;
	_ppPseudoRootFacade = &static_cast<boost::shared_ptr<QuantLib::PseudoRootFacade>>(childNative.currentLink());
    _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppPseudoRootFacade));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Models::CPseudoRootFacade::CPseudoRootFacade (QuantLib::PseudoRootFacade childNative)  : CMarketModel(CPseudoRootFacade::typeid)
{
#ifdef HANDLE
	_phPseudoRootFacade = NULL;
#endif
	_ppPseudoRootFacade = new boost::shared_ptr<QuantLib::PseudoRootFacade> (new QuantLib::PseudoRootFacade (childNative));
    _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppPseudoRootFacade));
}
#endif

Cephei::QL::Models::Marketmodels::Models::CPseudoRootFacade::~CPseudoRootFacade ()
{
    if (_ppPseudoRootFacade != NULL)
    {
	    delete _ppPseudoRootFacade;
        _ppPseudoRootFacade = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Models::CPseudoRootFacade::!CPseudoRootFacade ()
{
    if (_ppPseudoRootFacade != NULL)
    {
	    delete _ppPseudoRootFacade;
    }
}
QuantLib::PseudoRootFacade& Cephei::QL::Models::Marketmodels::Models::CPseudoRootFacade::GetReference ()
{
    if (_ppPseudoRootFacade == NULL) throw gcnew NativeNullException ();
	return **_ppPseudoRootFacade;
}
boost::shared_ptr<QuantLib::PseudoRootFacade>& Cephei::QL::Models::Marketmodels::Models::CPseudoRootFacade::GetShared ()
{
    if (_ppPseudoRootFacade == NULL) throw gcnew NativeNullException ();
	return *_ppPseudoRootFacade;
}
QuantLib::PseudoRootFacade* Cephei::QL::Models::Marketmodels::Models::CPseudoRootFacade::GetPointer ()
{
    if (_ppPseudoRootFacade == NULL) throw gcnew NativeNullException ();
	return &**_ppPseudoRootFacade;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::PseudoRootFacade>& Cephei::QL::Models::Marketmodels::Models::CPseudoRootFacade::GetHandle ()
{
	if (_phPseudoRootFacade == NULL)
	{
		_phPseudoRootFacade = new Handle<QuantLib::PseudoRootFacade> (*_ppPseudoRootFacade);
	}
	return *_phPseudoRootFacade;
}
#endif
bool Cephei::QL::Models::Marketmodels::Models::CPseudoRootFacade::HasNative () 
{
	return (_ppPseudoRootFacade != NULL);
}

Cephei::IVector<Double>^ Cephei::QL::Models::Marketmodels::Models::CPseudoRootFacade::Displacements::get ()
{
    try
    {
    	std::vector<QuantLib::Spread>& _rv = (std::vector<QuantLib::Spread>&)(*_ppPseudoRootFacade)->displacements ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::IEvolutionDescription^ Cephei::QL::Models::Marketmodels::Models::CPseudoRootFacade::Evolution::get ()
{
    try
    {
    	QuantLib::EvolutionDescription& _rv = (QuantLib::EvolutionDescription&)(*_ppPseudoRootFacade)->evolution ( );   
        Cephei::QL::Models::Marketmodels::CEvolutionDescription^ _nrv = gcnew Cephei::QL::Models::Marketmodels::CEvolutionDescription (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Models::Marketmodels::Models::CPseudoRootFacade::InitialRates::get ()
{
    try
    {
    	std::vector<QuantLib::Rate>& _rv = (std::vector<QuantLib::Rate>&)(*_ppPseudoRootFacade)->initialRates ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Models::CPseudoRootFacade::NumberOfFactors::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppPseudoRootFacade)->numberOfFactors ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Models::CPseudoRootFacade::NumberOfRates::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppPseudoRootFacade)->numberOfRates ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Models::CPseudoRootFacade::NumberOfSteps::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppPseudoRootFacade)->numberOfSteps ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IMatrix^ Cephei::QL::Models::Marketmodels::Models::CPseudoRootFacade::PseudoRoot (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
    	QuantLib::Matrix& _rv = (QuantLib::Matrix&)(*_ppPseudoRootFacade)->pseudoRoot ( _i );   
        Cephei::QL::Math::CMatrix^ _nrv = gcnew Cephei::QL::Math::CMatrix (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Models::IPseudoRootFacade^ Cephei::QL::Models::Marketmodels::Models::CPseudoRootFacade_Factory::Create (Cephei::IVector<Cephei::QL::Math::IMatrix^>^ covariancePseudoRoots, Cephei::IVector<Double>^ rateTimes, Cephei::IVector<Double>^ initialRates, Cephei::IVector<Double>^ displacements)
{
    return gcnew CPseudoRootFacade ( covariancePseudoRoots,  rateTimes,  initialRates,  displacements);
}
Cephei::QL::Models::Marketmodels::Models::IPseudoRootFacade^ Cephei::QL::Models::Marketmodels::Models::CPseudoRootFacade_Factory::Create (Cephei::QL::Models::Marketmodels::Models::ICTSMMCapletCalibration^ calibrator)
{
    return gcnew CPseudoRootFacade ( calibrator);
}
